#include "head.h"

ProgressBar::ProgressBar(QWidget * parent)
    :QWidget(parent)
{
//    setStyleSheet("QWiget {background-color: white;}");//设置样式
//        setAutoFillBackground(true);//执行填充
//        setGeometry(0,0,Widget::MAINWIDGET_WIDTH, ProgressBarHeight * 10);
    setFixedSize(Widget::MAINWIDGET_WIDTH, ProgressBarHeight);
    setAttribute(Qt::WA_TranslucentBackground, true);
    loadLayOut();
}
ProgressBar::~ProgressBar()
{

}

void ProgressBar::acceptMusicLength(double x)
{
    _musicTimeSec = x;
    setText(0);//重置为开始状态
    emit sendPointAndBarCdMusicProgress(0);//重置为开始状态
}


void ProgressBar::loadLayOut()
{
    auto vh = new QHBoxLayout();
    vh->setContentsMargins(20,0,10,0);//默认值是有边界的间距值，要设没有，必须主动设置
//    vh->setSpacing(2);
//    vh->setAlignment(Qt::AlignCenter);
    setLayout(vh);

    _leftTiming = new QLabel();
    _leftTiming->setFont(QFont(QFont("Arial",18,50)));
    _leftTiming->setFixedSize(50,25);
    _leftTiming->setStyleSheet("background: transparent; font-size: 18px; color: #ababab");
    vh->addWidget(_leftTiming, 0);

    _dot = new ProgressPointAndBar();
    connect(_dot, SIGNAL(musicTimePoint(double)), this, SLOT(acceptMusicProgress(double)));
    connect(this, SIGNAL(sendPointAndBarCdMusicProgress(double)), _dot, SLOT(acceptCdMusicProgress(double)));
    vh->addWidget(_dot, 0);

    _rightTiming = new QLabel();
    _rightTiming->setFont(QFont(QFont("Arial",18,50)));
    _rightTiming->setFixedSize(50,25);
    _rightTiming->setStyleSheet("background: transparent; font-size: 18px; color: #ababab");

    setText(0);
    vh->addWidget(_rightTiming, 0);

}

//进度条比例为rotia
void ProgressBar::setText(double rotia)
{
    int SecLeft = _musicTimeSec * rotia;
    int SecRight = _musicTimeSec - SecLeft;
    auto t = QTime(0, 0).addSecs(SecLeft);
    auto t2 = QTime(0,0).addSecs(SecRight);
    _leftTiming->setText(t.toString("mm:ss"));
    _rightTiming->setText(t2.toString("mm:ss"));
}

void ProgressBar::acceptCdMusicProgress(double rotia)
{
    setText(rotia);
    emit sendPointAndBarCdMusicProgress(rotia);
}

void ProgressBar::acceptMusicProgress(double rotia)
{
    setText(rotia);
    emit sendCdMusicProgress(rotia);
    emit sendVlcPlayerProgress(rotia);//设置vlc的播放进度
}

//class ProgressPointAndBar:public QWidget
//{
//    Q_OBJECT
//public:
//    ProgressPointAndBar(QWidget *parent = nullptr);
//    ~ProgressPointAndBar();
//private:
//    void mouseMoveEvent(QMouseEvent *event) override;
//    void mousePressEvent(QMouseEvent *event) override;
//    void mouseReleaseEvent(QMouseEvent *event) override;
//    void paintEvent(QPaintEvent *event) override;
//};

ProgressPoint::ProgressPoint(QWidget *parent)
    :QWidget(parent)
{
    setGeometry(0, (ProgressPointAndBar::Height - Width)/2, Width, Height);//一般是放在被挂的控件上设定,方便使用width()函数来设定，可以界面大小动态变化时随之变化
//    resize(Width, Height);
    setAttribute(Qt::WA_TranslucentBackground, true);
    _orginPos = pos();

}

void ProgressPoint::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        _lastMousePos = event->globalPos();
    }

}

//void ProgressPoint::mouseReleaseEvent(QMouseEvent *event)
//{

//}
void ProgressPoint::acceptMusicProgress(int x)
{
    emit moveX(x);
}


void ProgressPoint::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        auto diff = event->globalPos() - _lastMousePos;//必须要是全局坐标，pos会出问题
        emit moveX(diff.x());
        _lastMousePos = event->globalPos();
//        if(pos().x() + diff.x() >= 0 && pos().x() + diff.x() <= ProgressPointAndBar::Width - Width)
//        {
//            move(pos().x() + diff.x(), pos().y());


//        }
    }
}

void ProgressPoint::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter paintor(this);
    paintor.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing, true);
    paintor.save();
    QPixmap image(":/images/Resources/musicSeekButton.png");
    paintor.drawPixmap(rect(), image, image.rect());
    paintor.restore();
}

ProgressPoint::~ProgressPoint()
{

}


//class ProgressPointAndBar:public QWidget
//{
//    Q_OBJECT
//public:
//    ProgressPointAndBar(QWidget *parent = nullptr);
//    ~ProgressPointAndBar();
//    const constexpr static int Width = 344;
//    const constexpr static int Height = 14;//14;
//private slots:
//    void moveBar(int x);
//private:
//    void paintEvent(QPaintEvent *event) override;
//private:
//    ProgressPoint *_pot;
//      int _barMoveX;
//};

ProgressPointAndBar::ProgressPointAndBar(QWidget *parent)
    :QWidget(parent)
{
//    setStyleSheet("QLabel {background-color: white;}");//设置样式
//        setAutoFillBackground(true);//执行填充
    _pot = new ProgressPoint(this);
    _barMoveX = 0;
    connect(_pot, SIGNAL(moveX(int)), this, SLOT(moveBar(int)));
    setAttribute(Qt::WA_TranslucentBackground, true);
    resize(ProgressPointAndBar::Width, ProgressPointAndBar::Height);
}


void ProgressPointAndBar::acceptCdMusicProgress(double x)
{
    auto progress = x*(width() - _pot->Width) - _pot->pos().x();
    _barMoveX += progress;
    _pot->move(_pot->pos().x() + progress, _pot->pos().y());
    repaint();
}

void ProgressPointAndBar::moveBar(int x)
{
    if(_pot->pos().x() + x >= 0 && _pot->pos().x() + x <= (width() - _pot->Width))
    {
        _pot->move(_pot->pos().x() + x, _pot->pos().y());
        _barMoveX += x;
        auto ratio = static_cast<double>(_barMoveX)/static_cast<double>(width() - _pot->width());
        emit musicTimePoint(ratio);
        repaint();
    }else{
        if(_pot->pos().x() + x > (width() - _pot->Width))//在很多时候临界点附近如果不设置这种大于0的情况处理，会在临界点的附近点击无移动反应，原因是代表被移动控件的坐标是其的左上角，本身的宽度没有被放进去
        {
            _pot->move((width() - _pot->Width), _pot->pos().y());
            _barMoveX = (width() - _pot->Width);
            auto ratio = static_cast<double>(_barMoveX)/static_cast<double>(width() - _pot->width());
            emit musicTimePoint(ratio);
            repaint();
        }
    }
}

void ProgressPointAndBar::mouseReleaseEvent(QMouseEvent *event)
{
    auto x = event->pos().x();
    auto diffx = x - _pot->pos().x();
    moveBar(diffx);
}


void ProgressPointAndBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter paint(this);
    paint.setRenderHints(QPainter::SmoothPixmapTransform|QPainter::Antialiasing, true);
    paint.save();

    auto aimRect = QRect(0, (rect().height() - 14)/2, rect().width(), 14);
    QPixmap picture(":/images/Resources/musicProgressBg.png");
    paint.drawPixmap(aimRect, picture, picture.rect());

    paint.restore();
    paint.save();
    QPixmap picture2(":/images/Resources/musicProgressValue.png");
    auto color = picture2.toImage().pixelColor(10,10);
    auto brush = QBrush(color);
    paint.setBrush(brush);
    paint.setPen(color);
    paint.drawEllipse(QRect(aimRect.x(), aimRect.y(), aimRect.height(), aimRect.height()));//这个椭圆为什么这么规定？
    paint.drawRect(QRect(aimRect.height()/2, aimRect.y(), _barMoveX, aimRect.height()));
    paint.restore();


}




ProgressPointAndBar::~ProgressPointAndBar()
{

}





















